package com.semidata.trp.monitor.report;

import com.semidata.rtd.core.executor.TaskExecutor;
import com.semidata.rtd.core.job.Job;
import com.semidata.rtd.core.job.ScheduledTask;
import com.semidata.rtd.core.service.ServiceManager;
import com.semidata.trp.monitor.job.MonitorJobContext;
import com.semidata.trp.monitor.job.MonitorRequest;

public class TaskReport extends ScheduledTask<Void, MonitorJobContext> {

	public TaskReport(Job<MonitorJobContext> job, TaskExecutor executor) {
		super(job, executor);
	}

	@Override
	public Void run() {
		MonitorJobContext context = getJob().getJobContext();
		MonitorRequest request = context.getMonitorRequest();

		ReportService reportService = ServiceManager
				.getService(ReportService.class);
		ReportData data = reportService.getData();
		if (request.getType() == MonitorRequest.Type.IMP) {
			data.updateOrganizationCosts(request.getOrgId(),
					request.getMediaType(), request.getPrice() / 1000.0);
			data.updateStrategyCosts(request.getStrategyId(),
					request.getMediaType(), request.getPrice() / 1000.0);
			data.updateResourceImpressions(request.getResourceId(), 1);
			data.updateStrategyImpressions(request.getStrategyId(),
					request.getMediaType(), 1);
		} else {
			data.updateResourceClicks(request.getResourceId(), 1);
			data.updateStrategyClicks(request.getStrategyId(), 1);
		}
		data.updateKeyWordList(request.getKeyWord());
		return null;
	}

	@Override
	protected void done(Void result, Throwable cause) throws Exception {
	}

}
